package com.jvm123.demo.controller;

import com.jvm123.demo.entity.Movie;
import com.jvm123.demo.entity.Post;
import com.jvm123.demo.service.MovieMeilisearchRepository;
import com.jvm123.demo.service.PostMeilisearchRepository;
import com.jvm123.meilisearch.json.SearchResult;
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.SearchRequest;
import com.meilisearch.sdk.UpdateStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author yawn http://jvm123.com
 * 2020/1/15 17:17
 */
@RestController
public class TestController {

    @Resource
    Client client;

    @Resource
    private PostMeilisearchRepository postRepository;
    @Resource
    private MovieMeilisearchRepository movieRepository;


    @GetMapping("test")
    public void get() throws Exception {

        Index[] indexList = client.getIndexList();
        Index index = postRepository.getIndex();

        long updateId1 = postRepository.add(new Post("1", "title", "content", new Date()));
        long updateId2 = postRepository.add(new Post("1", "使用技巧", "搜索查询出结果，访问href，再使用下载器下载", new Date()));
//        long updateId3 = postRepository.delete("1");
        UpdateStatus[] updates = postRepository.getUpdates();

        System.out.println(updates);
    }

    @GetMapping("test2")
    public SearchResult get2() throws Exception {


        SearchRequest sr = new SearchRequest("喵星人")
                .setMatches(true)
                .setAttributesToHighlight(new String[]{"name", "title", "des", "time"});
        SearchResult<Movie> searchResult = movieRepository.search(sr);
        List<Movie> hits = searchResult.getHits();
        System.out.println(hits);
        return searchResult;
    }

}
